package logic

import (
	"context"
	"develop/model/mysql"
	"errors"
	"gorm.io/gorm"

	"develop/usersrpc/internal/svc"
	"develop/usersrpc/usersrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type VideoAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewVideoAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *VideoAddLogic {
	return &VideoAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *VideoAddLogic) VideoAdd(in *usersrpc.VideoAddRequest) (*usersrpc.VideoAddResponse, error) {
	// todo: add your logic here and delete this line
	var findUserId mysql.User_info
	err := findUserId.FindId(mysql.DB, in.UserId)
	if err != nil {
		return nil, err
	}
	if findUserId.ID == 0 {
		return nil, errors.New("用户不存在")
	}
	video := &mysql.Video_Info{
		Model:          gorm.Model{},
		Upload_User_Id: int(in.UserId),
		Video_Title:    in.Video_Title,
		Video_Category: in.Video_Category,
		Video_Link:     in.VideoLink,
		Cover_Link:     in.CoverLink,
		Video_Intro:    in.Video_Intro,
		Play_Num:       int(in.Play_Num),
		Video_Status:   int(in.VideoStatus),
		Topic_Id:       int(in.TopicId),
	}
	err = video.VideoCreate(mysql.DB)
	if err != nil {
		return nil, err
	}
	return &usersrpc.VideoAddResponse{
		VideoId: int64(video.ID),
	}, nil
}
